package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id,
                          @RequestHeader(value = "TruthKey",required =false)String truth) throws InterruptedException {
     /*   if (id ==1 ){
            //触发熔断
            Thread.sleep(60);
        }else if (id ==2 ){
            throw new RuntimeException("故意抛出异常，异常触发比例熔断");
        }*/
        /*打印请求头*/
        System.out.println("truth---->"+truth);
        return userService.queryById(id);
    }

    @Autowired
    private PatternProperties patternProperties;

   /* @Value("${pattern.dateformat}")
    private String dateformat;*/
    /**
     * 测试读取bootstrap.yml文件信息
     * @return
     */
    @GetMapping("/now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }
    @Autowired
    private PatternProperties properties;
    @GetMapping("prop")
    public PatternProperties  properties(){
        return properties;
    }

}
